<template>
  <view class="message-page">
    <view class="custom-navbar navbar-fixed">
      <view class="navbar-title">消息</view>
    </view>
    <view class="message-content">
      <view v-if="messages.length === 0" class="empty-message">暂无消息</view>
      <view v-else>
        <view
          v-for="msg in messages"
          :key="msg.id"
          class="msg-list-item"
          :class="{ unread: msg.unread }"
          @click="goToDetail(msg)"
        >
          <view class="msg-icon">
            <u-icon
              v-if="!msg.avatar"
              :name="getIconName(msg.icon)"
              size="40"
              :color="getIconColor(msg.type)"
            />
            <image v-else :src="msg.avatar" class="msg-avatar" />
          </view>
          <view class="msg-main">
            <view class="msg-title-row">
              <text class="msg-title">{{ msg.title }}</text>
              <text v-if="msg.tag" class="msg-tag">{{ msg.tag }}</text>
              <view v-if="msg.unread" class="msg-dot"></view>
            </view>
            <view class="msg-content">{{ msg.content }}</view>
          </view>
          <view class="msg-time">{{ msg.time }}</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      allRedDot: 0,
      messages: [
        {
          id: 1,//
          type: "order",
          icon: "order",//
          title: "订单消息",
          content: "你的订单已完结",
          time: "3分钟前",
          unread: true,
        },
        {
          id: 2,
          type: "interaction",
          icon: "chat",
          title: "互动消息",
          content: "@用户1008600评论了你的内容",
          time: "5小时前",
          unread: true,
        },
        {
          id: 3,
          type: "system",
          icon: "info-circle",
          title: "系统消息",
          content: "你提交的个人信息审核通过",
          time: "23小时前",
          unread: false,
          tag: "官方",
        },
        {
          id: 4,
          type: "game",
          icon: "account",
          title: "果汁分你一半",
          content: "游记消息",
          time: "2天前",
          unread: false,
          avatar: "https://cdn.uviewui.com/uview/album/1.jpg",
        },
        {
          id: 5,
          type: "friend",
          icon: "account",
          title: "飞来飞去",
          content: "老想来北京玩啊~",
          time: "1周前",
          unread: false,
          avatar: "https://cdn.uviewui.com/uview/album/2.jpg",
        },
      ],
    };
  },
  onShow() {
    this.allRedDot = 2;
    if (this.allRedDot > 0) {
      uni.showTabBarRedDot({
        index: 2,
      });
    }
    const isLogin = uni.getStorageSync("isLoggedIn") || false;
  },
  methods: {
    getIconName(type) {
      // uView内置icon名
      if (type === "order") return "order";
      if (type === "chat" || type === "interaction") return "chat";
      if (type === "info-circle" || type === "system") return "info-circle";
      return "account";
    },
    getIconColor(type) {
      if (type === "order") return "#ff4d4f";
      if (type === "interaction") return "#ff9900";
      if (type === "system") return "#2979ff";
      return "#888";
    },
    goToDetail(msg) {
      uni.navigateTo({
        url: `/pages/message/message-detail?id=${msg.id}`,
      });
    },
  },
};
</script>

<style scoped>
:root {
  --primary-color: #007aff;
}
.message-page {
  min-height: 100vh;
  background: #f8f9fb;
}
.custom-navbar.navbar-fixed {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 120rpx;
  z-index: 10;
  background: linear-gradient(90deg, #4f8cff 0%, #007aff 100%);
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 35rpx 0;
}
.navbar-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
  text-align: center;
  letter-spacing: 2rpx;
}
.message-content {
  padding-top: 120rpx;
  min-height: 80vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}
.empty-message {
  color: #bbb;
  font-size: 28rpx;
  margin-top: 80rpx;
}
.msg-list-item {
  display: flex;
  align-items: flex-start;
  background: #fff;
  border-radius: 16rpx;
  margin-bottom: 24rpx;
  padding: 24rpx 20rpx;
  box-shadow: 0 2rpx 8rpx #eee;
  position: relative;
  width: 670rpx;
}
.msg-list-item.unread {
  box-shadow: 0 4rpx 16rpx #e6f7ff;
}
.msg-icon {
  margin-right: 20rpx;
  width: 56rpx;
  height: 56rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}
.msg-avatar {
  width: 56rpx;
  height: 56rpx;
  border-radius: 50%;
}
.msg-main {
  flex: 1;
  min-width: 0;
}
.msg-title-row {
  display: flex;
  align-items: center;
}
.msg-title {
  font-size: 30rpx;
  font-weight: 500;
  margin-right: 12rpx;
}
.msg-tag {
  font-size: 20rpx;
  color: #2979ff;
  background: #e6f0ff;
  border-radius: 8rpx;
  padding: 2rpx 10rpx;
  margin-right: 8rpx;
}
.msg-dot {
  width: 14rpx;
  height: 14rpx;
  background: #ff4d4f;
  border-radius: 50%;
  margin-left: 8rpx;
}
.msg-content {
  font-size: 26rpx;
  color: #888;
  margin-top: 6rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-time {
  font-size: 22rpx;
  color: #bbb;
  margin-left: 16rpx;
  white-space: nowrap;
}
</style>